package com.example.module_web.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.module_web.viewholder.FormViewHolder;

import java.util.HashMap;
import java.util.List;

import static android.content.ContentValues.TAG;

/**
 * 表格适配器
 * T ：表格实体类-beans
 */

public abstract class FormAdapter<T> extends RecyclerView.Adapter<FormViewHolder> implements FormViewHolder.FormItemLayout {
    private static final String TAG = "bug";
    private Context mContext;
    private List<T> mItems;
    private T form;
    private View mViewHolder;
    //字段键名
    final String NUM = "num";
    final String ACCOUNT = "account";
    final String PRODUCT = "product";
    final String ADDRS = "addrs";
    final String DATE = "date";
    final String PAYABLEPRICE = "payableprice";
    final String PAIDPRICE = "paidprice";
    final String LABEL = "label";
    final String POSTAGE = "postage";
    final String WEIGHT = "weight";
    final String REMARK = "remark";
    final String ALLPOINTS = "allpoints";
    final String USEPOINTS = "usepoints";
    final String SALEPRICE = "saleprice";
    final String INVENTORY = "inventory";
    final String RETAILPRICE = "retailprice";
    final String SPECIFICATION = "specification";
    final String ACTIVE = "active";


    public FormAdapter(Context context, List<T> items){
        this.mContext = context;
        this.mItems = items;
    }

    @Override
    public FormViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(setItemLayoutId(), parent, false);
        Log.e(TAG, "onCreateViewHolder: 6");
        return new FormViewHolder(view,this);
    }

    /**
     * 设置表格子项布局ID
     * @return 布局ID
     */
    protected abstract int setItemLayoutId();


    @Override
    public void onBindViewHolder(FormViewHolder holder, int position) {
        Log.e(TAG, "onBindViewHolder: 7");
        Object o = mItems.get(position);
        HashMap<String, View> itemViewIds = holder.getItemViewIds();
        onBindFormData(itemViewIds,o);
        Log.e(TAG, "onBindViewHolder: 8");
    }

    /**
     * 把实体类绑定到表格子项中
     * @param holder HashMap集合 键：子项中view的代号，值：子项view的实例
     * @param o 表格对应的实体类
     */
    protected abstract void onBindFormData(HashMap<String, View> holder, Object o);

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public T addItem(List<T> items){
        try{
            items.add(0,form);
        }catch (NullPointerException ignored){
            Log.e(TAG, "addItem: 数据为空");
            ignored.printStackTrace();
        }
        notifyDataSetChanged();
        return form;
    }

}
